//
//  videoViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-15.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "videoViewController.h"
#import "videoView.h"
#import "videoDetailViewController.h"
#import "AvdownLoad.h"
#import <MediaPlayer/MediaPlayer.h>

@interface videoViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation videoViewController


- (void)dealloc
{
    [_vid release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        
          self.vid = [[[videoView alloc] initWithFrame:CGRectMake(0,89, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 89 - 44)] autorelease];
    }
    return self;
}


-(void)loadView{

    self.view = self.vid;

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.vid.videoTab.delegate = self;
    
    self.vid.videoTab.dataSource = self;
    //注册cell
    
    [self.vid.videoTab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell_id"];
   
    //反归档
    [[AvdownLoad Sharedown] unAchiver];
    //通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(arrayChange) name:@"titleArray" object:nil];
    
    
    // Do any additional setup after loading the view.
}

//
-(void)arrayChange{

  [self.vid.videoTab reloadData];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if ([AvdownLoad Sharedown].titleArray.count == 0) {
        return 1;
    }else{
    
       return [AvdownLoad Sharedown].titleArray.count;
    }

    
  
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
   
        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath ];
           
   
    if ([AvdownLoad Sharedown].titleArray.count == 0) {
       
        cell.textLabel.text = @"暂无下载内容";
    }else{
        
       //字符串替代
        //NSString *sr = [[AvdownLoad Sharedown].titleArray[indexPath.row] stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
       cell.textLabel.text = [AvdownLoad Sharedown].titleArray[indexPath.row] ;
    }


   
    
    return cell;

    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if ([AvdownLoad Sharedown].titleArray.count == 0) {
        
        
    }else{
        
        
        NSString *str = [AvdownLoad Sharedown].titleArray[indexPath.row];
        
        
        NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
        
        MPMoviePlayerViewController *playerViewController = [[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_vedio.mp4",str]]]] autorelease];
     
        [self presentMoviePlayerViewControllerAnimated:playerViewController];
        
    }

    
 
    

}
//确定编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return _vstyle;
    
}

//4.完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        self.inter = indexPath.row;
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //判断数组为空,无法删除(没有下载内容)
        if ([AvdownLoad Sharedown].titleArray.count == 0){
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"亲,暂无下载内容" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            
            
            [alter show];
            [alter dismissWithClickedButtonIndex:0 animated:YES];
            [alter release];
            
            }else{
        
                //判断是否下载完成即正在下载
            if (![AvdownLoad Sharedown].flag) {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"下载任务已经存在,确定要删除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
                    
            [alter show];
            }else{
            
            
                
                //删除沙盒里路径下的下载的视频
                
                NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
                NSString *str = [AvdownLoad Sharedown].titleArray[indexPath.row];
                
                [[NSFileManager defaultManager] removeItemAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_vedio.mp4",str]] error:nil];
                
                
                //删除沙盒路径下plist文件下的内容
                
                [[AvdownLoad Sharedown] delete:[AvdownLoad Sharedown].titleArray[indexPath.row]];
                
                [AvdownLoad Sharedown].flag = NO;
            
            }
       
        }
        
    }
}



//弹框视图点击按钮时调用,正在下载时也可删除
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
     
        [AvdownLoad Sharedown].recriverData = nil;
        
        
        //删除沙盒里路径下的下载的视频
        
        NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
        NSString *str = [AvdownLoad Sharedown].titleArray[self.inter];
        
        [[NSFileManager defaultManager] removeItemAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_vedio.mp4",str]] error:nil];
        
        //删除沙盒路径下plist文件下的内容
        
        [[AvdownLoad Sharedown] delete:[AvdownLoad Sharedown].titleArray[self.inter]];
        
        [AvdownLoad Sharedown].flag = NO;
        
        
        
    }
        
   
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
